<template>
  <div class="login">
    <el-container>
      <!-- 头部 -->
      <el-header class="bg-green1 text-white text-bl pl-0 d-flex ai-center z-2">
        <div class="header-left">
          <i class="iconfont">&#xe66d;</i>
          <span class="letter-s">中药材管理信息系统</span>
        </div>
      </el-header>
      <!-- 主体 -->
      <el-main class="p-0 z-2">

        <div class="login-box bg-gary">
          <!-- 头像 -->
          <div class="avater-box bg-gary">
            <i class="iconfont login-head">&#xe60b;</i>
          </div>
          <!-- 登录表单 -->
          <el-form ref="loginFormRef" :model="loginForm" label-width="0" class="login-form" :rules="loginFormRules">
            <!-- 用户名 -->
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-lock"> </el-input>
            </el-form-item>
            <!-- 按钮 -->
            <el-form-item class="d-flex jc-end">
              <el-button type="primary" @click="login">登录</el-button>
              <el-button type="info" @click="reset">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        loginForm: {
          username: 'root',
          password: '123456'
        },
        loginFormRules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      reset() {
        this.$refs.loginFormRef.resetFields()
      },
      login() {
        this.$refs.loginFormRef.validate(async valid => {
          if(!valid) return;
          const { data: res } = await this.$http.post('/login', this.loginForm)
          console.log(res)
          if(res.status === false) return this.$message.error('登录失败！')
          this.$message.success('登录成功!')
          window.sessionStorage.setItem('token', res.token)
          this.$router.push('/home')
        })

      }
    }
  }
</script>

<style lang="scss" scoped>
  .login {
    height: 100%;
    background-image: url(../assets/img/bg.jpg);
    background-size: cover;
    background-position: center;

    .login-box {
      width: 450px;
      height: 300px;
      border-radius: 3px;
      position: absolute;
      right: 10%;
      top: 50%;
      transform: translate(0, -50%);
      box-shadow: 0px 0px 30px 2px rgba(0, 0, 0, 0.5);

      .avater-box {
        width: 130px;
        height: 130px;
        border-radius: 50%;
        box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: #FFFFFF;
      }
      .login-form {
        position: absolute;
        width: 100%;
        bottom: 0;
        padding: 0 30px;
      }
    }

  }

  .login:after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.4);
  }
  .login-head{
    height: 130px;
    width: 130px;
    line-height: 130px;
    font-size: 120px;
    background-color: #a4cab6;
    border-radius: 50%;
    color: white;
    text-align: center;
  }
  .header-left i {
    color: white;
    margin: 0 20px;
  }
</style>
